package router

import (
	"fmt"
	"goGin/model"
	"goGin/tools"

	"github.com/garyburd/redigo/redis"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func Index(r *gin.Engine, db *gorm.DB, d redis.Conn) { //登录
	r.POST("/login", func(c *gin.Context) {
		var Json model.Query
		// token, _ := c.Get("tokendata")
		// fmt.Print(token)
		if err := c.ShouldBindJSON(&Json); err != nil {
			fmt.Print(err.Error())
			c.JSON(200, gin.H{
				"state": 500,
				"msg":   "参数错误",
			})
			return
		}
		// fmt.Print(Json.Query)
		if Json.Query["phone"] == nil {
			c.JSON(200, gin.H{
				"state": 400,
				"msg":   "手机号不能为空",
			})
			return
		}
		if Json.Query["password"] == nil {
			c.JSON(200, gin.H{
				"state": 400,
				"msg":   "用户密码不能为空",
			})
			return
		}
		models := []model.User{}
		query := Json.Query
		// fmt.Print(query)
		queryuser := map[string]interface{}{
			"phone": query["phone"],
		}
		result := db.Where(queryuser).Find(&models)
		if result.Error != nil {
			// models = []model.User{}
			c.JSON(200, gin.H{
				"state": 500,
				"msg":   "查询错误",
			})
			return
		}
		if len(models) == 0 {
			c.JSON(200, gin.H{
				"state": 400,
				"msg":   "用户不存在",
			})
			return
		}
		models = []model.User{}
		query["password"] = tools.Md5(query["password"].(string))
		results := db.Where(query).Find(&models)
		if results.Error != nil {
			// models = []model.User{}
			c.JSON(200, gin.H{
				"state": 500,
				"msg":   "查询错误",
			})
			return
		}
		if len(models) == 0 {
			c.JSON(200, gin.H{
				"state": 400,
				"msg":   "用户密码错误",
			})
			return
		}
		tokens := tools.Settoken(models[0])
		c.JSON(200, gin.H{
			"state": 200,
			"msg":   "登陆成功",
			"data":  models,
			"token": tokens,
		})
	})
	r.POST("/setlogin", func(c *gin.Context) { //注册
		var Json model.Query
		if err := c.ShouldBindJSON(&Json); err != nil {
			// fmt.Print(err.Error())
			c.JSON(200, gin.H{
				"state": 500,
				"msg":   "参数错误",
			})
			return
		}
		// fmt.Print(Json.Query)
		if Json.Query["phone"] == nil {
			c.JSON(200, gin.H{
				"state": 400,
				"msg":   "手机号不能为空",
			})
			return
		}
		if Json.Query["password"] == nil {
			c.JSON(200, gin.H{
				"state": 400,
				"msg":   "用户密码不能为空",
			})
			return
		}
		models := []model.User{}
		query := Json.Query
		// fmt.Print(query)
		queryuser := map[string]interface{}{
			"phone": query["phone"],
		}
		result := db.Where(queryuser).Find(&models)
		if result.Error != nil {
			// models = []model.User{}
			c.JSON(200, gin.H{
				"state": 500,
				"msg":   "查询错误",
			})
			return
		}
		if len(models) > 0 {
			c.JSON(200, gin.H{
				"state": 400,
				"msg":   "用户已存在",
			})
			return
		}
		phoness := int(query["phone"].(float64)) //转int

		isphone, err := tools.Isphone(phoness)
		if isphone == false {
			c.JSON(200, gin.H{
				"state": 400,
				"msg":   err,
			})
			return
		}
		//MD5加密
		md5str := tools.Md5(query["password"].(string))
		// fmt.Print(phss, organpassword)
		strphone, _ := tools.Forstring(phoness)
		dataquey := &model.User{
			Phone:    phoness,
			Password: md5str,
			Username: strphone + "newUser",
			Top:      "http://" + c.Request.Host + "/static/defail.jpg",
		}
		if result := db.Create(dataquey); result.Error != nil {

			c.JSON(200, gin.H{
				"state": 400,
				"msg":   "数据写入错误",
			})
			return
		}
		c.JSON(200, gin.H{
			"state": 200,
			"msg":   "注册成功",
		})
	})
}
